<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="../../static/font-awesome-4.7.0/css/font-awesome.min.css" >
    <link rel="stylesheet" href="../../static/vui/css/vui.css">

    <script type="text/javascript" src="../../static/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../../static/layer/layer.js"></script>
    <script type="text/javascript" src="../../static/laydate/laydate.js"></script>
    <script type="text/javascript" src="../../static/vui/js/vui.js"></script>
    <script type="text/javascript" src="../../static/vui/js/datagrid.js"></script>
    <script type="text/javascript" src="../../static/js/common.js"></script>
</head>
<body>

<div id="formArea">
    <table class="table-top">
        <tr>
            <td>
                <label class="label">单据日期</label>
            </td>
            <td>
                <input type="text" n="date" class="input search-date" name="dateStart" autocomplete="off"/>&nbsp;-&nbsp;
                <input type="text" n="date" class="input search-date" name="dateEnd" autocomplete="off"/>
            </td>
            <td>
                <label>单据编号</label>
            </td>
            <td>
                <input type="text" class="input input-query" name="code" autocomplete="off"/>
            </td>
            <td>
                <label>商品</label>
            </td>
            <td>
                <div id="productId"></div>
            </td>
            <td>
                <label>供应商</label>
            </td>
            <td>
                <div id="relationId"></div>
            </td>
            <td>
                <button class="button" id="query"><span>查询</span></button>
                <button class="button" id="reset"><span>重置</span></button>
            </td>
        </tr>
    </table>
</div>
<div id="table"></div>
<script>
    $(function(){
        $('#query').click(function(){
            $("#table").datagrid("reload",app.serializeForm());
        });

        var nowDate = app.time2str(new Date()),startDate = new Date(nowDate.substr(0,8)+'01');
        dateStart = laydate.render({
            elem: 'input[name="dateStart"]',value:startDate,max:nowDate,
            done: function(value,date){
                if(value){
                    console.log(date);
                    date.month=date.month-1;
                    dateEnd.config.min = date;
                }else{
                    dateEnd.config.min=dateStart.config.min;
                }
            }
        });
        dateEnd = laydate.render({
            elem: 'input[name="dateEnd"]',value:nowDate,min:nowDate.substr(0,8)+'01',
            done: function(value,date){
                if(value){
                    date.month=date.month-1;
                    dateStart.config.max = date;
                }else{
                    dateStart.config.max=dateEnd.config.max;
                }
            }
        });

        $('#reset').click(function () {
            $('#formArea input').val('');

            var endDate = app.time2str(new Date());
            var startDate = endDate.substr(0,8)+'01';
            $('input[name="dateStart"]').val(startDate);

            dateEnd.config.min = {year:getYear(startDate),month:getMonth(startDate),date:getDate(startDate)};

            $('input[name="dateEnd"]').val(endDate);
            dateStart.config.max = {year:getYear(endDate),month:getMonth(endDate),date:getDate(endDate)};


        });

        function getYear(dateStr){
            return parseInt(dateStr.split('-')[0]);
        }

        function getMonth(dateStr){
            return parseInt(dateStr.split('-')[1])-1;
        }

        function getDate(dateStr){
            return parseInt(dateStr.split('-')[2]);
        }

        $('#relationId').search({
            name:'relationId',width:160,
            defaultValue:'',
            click : function(){
                app.window(layer,{
                    title:'供应商选择',
                    btn:[],
                    area: app.relationArea,
                    content: '../common/vendor_list.html?index=-1&searchId=relationId'
                });
            }
        });

        $('#productId').search({
            name:'productId',width:160,
            click:function(){
                // this click before blur
                var index = layer.getFrameIndex(window.name);
                app.window(layer,{
                    title:'商品选择',
                    btn:[],
                    area: app.productArea,
                    content: '../common/product_list_query.html?index='+index+'&searchId=productId'
                });
            }
        });

        $('#table').datagrid({
            statistics:true,pagination:false,
            queryParams:{dateStart:startDate.getTime(),dateEnd:new Date().getTime()},
            url:app.BASE_API_URL+'report/purchaseDetail',
            columns:[
                {field:'purchaseDate',title:'采购日期',width:200,n:'date'},
                {field:'purchaseCode',title:'采购单编号',width:200,fixed:true,formatter:function (row) {
                    if(row.orderType==3){
                        return '<a dg="1" class="a">CG'+row.purchaseCode+"</a>";
                    }else{
                        return '<a dg="1" class="a">CT'+row.purchaseCode+"</a>";
                    }
                }},
                {field:'orderType',title:'类别',width:200,formatter:function (row) {
                        return row.orderType == 3 ? app.getLable('进货','success'):row.orderType == 4 ? app.getLable('退货','warning'):"";
                    }},
                {field:'vendorName',title:'供应商',width:200},
                {field:'productCode',title:'商品编号',width:200},
                {field:'productName',title:'商品名称',width:200},
                {field:'model',title:'规格型号',width:200},
                {field:'unitName',title:'单位',width:200},
                {field:'storeName',title:'仓库',width:200},
                {field:'number',title:'数量',n:'number',count:true,width:200},
                {field:'price',title:'单价',n:'money',width:180,count:'avg'},
                {field:'discountPrice',title:'实际付款金额',n:'money',count:true,width:200},
                {field:'discountMoney',title:'折扣金额',n:'money',count:true,width:200}
            ],
            loadSuccess:loadDataSuccess
        });
    });

    function loadDataSuccess(){
        $('.a[dg="1"]').click(function(){
            var row = $(this).parent().parent().data();

            if(row.orderType==3){
                var href = "purchase/buy/buy_add.html?code="+row.purchaseCode+"&id="+row.id+"&status="+row.status+"&orderType=3";
                top.menuClick({id:102,name:'采购单',href:href,center:true});
            }else if(row.orderType==4){
                var href = "purchase/rebuy/rebuy_add.html?code="+row.purchaseCode+"&id="+row.id+"&status="+row.status+"&orderType=4";
                top.menuClick({id:103,name:'采购退货单',href:href,center:true});
            }

        });
    }



</script>

</body>
</html>